package at.socialconference.web.server.util;

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
//TODO what happens if a user denies access to webpage, we still try to get authentication with invalid authtoken?!
public class TwitterHelper {

	public final static String CONSUMER_KEY = "6VTFnlk9V4ZvGlhREQoKYg";
	public final static String CONSUMER_SECRET = "cMqcPBf8S9yU1E56n6MUDEhs5KQ33kRCfWehvqagjSE";

	@SuppressWarnings("unused")
	private boolean gotConnection=false;
	private AccessToken accessToken;
	private RequestToken requestToken;

	private Twitter twitter;
	
	public TwitterHelper() {
		this.twitter = new TwitterFactory().getInstance();
		this.twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
	}
	

	public String getAuthURL() {
		requestToken = null;
		try {
			requestToken = twitter.getOAuthRequestToken();
		} catch (TwitterException e) {
			if (401 == e.getStatusCode()) {
				e.printStackTrace();
				System.out.println("Hot der sekkl iatz 2mol gedruckt?");
				return "stress nit";
			} else {
				e.printStackTrace();
			}
		}

		return requestToken.getAuthorizationURL();
	}
	/**
	 * creates AccessToken AND gets an authorized instance of Twitter!
	 * @param token
	 * @param tokenSecret
	 * @return
	 */
	public void createAccToken(String token, String tokenSecret) {
		accessToken = new AccessToken(token, tokenSecret);
		
		TwitterFactory factory = new TwitterFactory();
		twitter = factory.getOAuthAuthorizedInstance(
				CONSUMER_KEY, CONSUMER_SECRET, accessToken);
		return;
	}

	/**
	 * as soon as user has pin this method is called and an access token should
	 * be created and ev. saved in DB for every user.
	 * 
	 * @param pin
	 */
	public Integer createAccToken(String pin) {
		try {
			if (pin.length() > 0) {
				accessToken = twitter.getOAuthAccessToken(requestToken, pin);
				return 1;
			} else {
				accessToken = twitter.getOAuthAccessToken();
				return 1;
			}
		} catch (TwitterException te) {
			if (401 == te.getStatusCode()) {
				te.printStackTrace();
				System.out.println("#####Fehler, i kriag AccessToken nit -createAccessToken!");
				return -2;
			} else {
				System.out.println("#####Fehler mit accessToken (pin?)");
				te.printStackTrace();
				return -1;
			}
		}catch(IllegalStateException ies){
			System.out.println("#####Fehler mit accessToken, no token available");
			ies.printStackTrace();
			return -3;
		}

	}

	/**
	 * gimme id and i will follow you
	 * 
	 * @param id
	 * @return
	 */
	public Integer follow(int id) {
		try {
			twitter.createFriendship(id);
			return 1;
		} catch (TwitterException e) {
			if (403 == e.getStatusCode()) {
				System.out.println("-----Already friends -follow!");
				return 2;
			} else if(401 == e.getStatusCode()){
				System.out.println("-----App Access Denied -follow?!");
				return -2;
			}else {
				e.printStackTrace();
				return -1;
			}
		}
	}
	
	/**
	 * gimme id and i will unFollow you
	 * 
	 * @param id
	 * @return
	 */
	public Integer unFollow(int id) {
		try {
			twitter.destroyFriendship(id);
			return 1;
		} catch (TwitterException e) {
			if (403 == e.getStatusCode()) {
				System.out.println("-----never been friends -unfollow?");
				return 2;
			}else if(401 == e.getStatusCode()){
				System.out.println("-----App Access Denied -unfollow?!");
				return -2;
			} else {
				e.printStackTrace();
				return -1;
			}
		}
	}

	/**
	 * getting timelines of all the friends
	 * they get cut down to timelines of conferences int the SCwebServiceImpl
	 * @param id
	 * @return
	 */
	// TODO nur timelines von den conferencen holen, andere rausschneiden!
	public List<Status> getTimelines() {
		List<Status> statuses = null;
		try {
			statuses = twitter.getFriendsTimeline();
		} catch (TwitterException e) {
			if(401 == e.getStatusCode()){
				System.out.println("-----App Access Denied getTimelines?!\n");
				return null;
			}
			e.printStackTrace();
		}
		
		return statuses;
	}

	public AccessToken getAccessToken() {
		return accessToken;
	}
	
	public int[] getFriends(){
		int[] ids=null;
		try {
			ids =twitter.getFriendsIDs().getIDs();
		} catch (TwitterException e1) {
			if(401 == e1.getStatusCode()){
				System.out.println("-----App Access Denied -getfriends?!\n");
				return null;
			}
			e1.printStackTrace();
		}
		return ids;
		
	}

}
